package net.xdclass.service;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.annotation.RepeatSubmit;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.function.Function;

/**
 * @author Allen
 * @version 1.0
 * @date 2022/11/22 09:21
 */

@Service
@Slf4j
public class RepeatTypeFactory {


	@Resource
	private RepeatSubmitHandler repeatSubmitHandler;
	private HashMap<RepeatSubmit.Type, Function<ProceedingJoinPoint, Boolean>> repeatTypeMap = new HashMap();

	@PostConstruct
	public void init() {
		repeatTypeMap.put(RepeatSubmit.Type.PARAMS, proceedingJoinPoint -> repeatSubmitHandler.byLock(proceedingJoinPoint));
		repeatTypeMap.put(RepeatSubmit.Type.TOKEN, proceedingJoinPoint -> repeatSubmitHandler.byToken(proceedingJoinPoint));

	}


	public Boolean getResult(RepeatSubmit.Type type,ProceedingJoinPoint proceedingJoinPoint) {

		Function<ProceedingJoinPoint, Boolean> result = repeatTypeMap.get(type);

		if (ObjectUtil.isNotNull(result)) {

			return result.apply(proceedingJoinPoint);
		}


		log.error("没有找到对应的策略");
		return Boolean.FALSE;
	}


}
